
/* GCSx
** WORLDPLAY.H
**
** World storage- gameplay-only functionality
*/

/*****************************************************************************
** Copyright (C) 2003-2006 Janson
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
** 
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
*****************************************************************************/

#ifndef __GCSx_WORLDPLAY_H_
#define __GCSx_WORLDPLAY_H_

class WorldPlay : public World {
private:
    // ALL Entities and Sprites, indexed by id
    EntityIndex entitysById;
    SpriteIndex spritesById;

    // The last entity/sprite ID we assigned
    int lastEntityId;
    int lastSpriteId;
    
    // Current scene is locked
    Scene* currentScene;
    
    // Scenes that we've handled initial spawning for (a one-time event)
    std::set<int> scenesSpawned;
    void spawnScene(Scene* scene);
    
    // Call after buildScripts() only!
    // Call if no errors only!
    // Locks EVERY library, and links them
    // Locks every SCRIPT too, if config setting enabled
    void lockLibraries();
    void unlockLibraries();
    int librariesLocked;
    int allScriptsLocked;

public:
    // Opens an existing world for gameplay
    WorldPlay(const char* wFilename) throw_File;
    virtual ~WorldPlay();
    
    // Prepare game for play, with starting scene selected
    // OK to call before initializing GL
    // Undefined behavior if called after game started, etc.
    // Throws if any errors and game should be aborted
    void gameStart() throw_File;
    
    // Working with entities
    // Indexed entities are owned by WORLD and deleted by it for simplicity
    // (since there is currently no way to add/delete scenes/layers mid-game)
    void indexEntity(Entity* addEntity);
    void deindexEntity(Entity* remEntity);
    Entity* findEntity(int fId) const;
    int unusedEntityId();
    
    // Working with sprites
    // Indexed sprites are owned by WORLD and deleted by it for simplicity
    // (since there is currently no way to add/delete scenes/layers mid-game)
    void indexSprite(Sprite* addSprite);
    void deindexSprite(Sprite* remSprite);
    Sprite* findSprite(int fId) const;
    int unusedSpriteId();

    // Load a new current scene; NULL for none
    void changeScene(Scene* newScene);
    Scene* getCurrentScene() { return currentScene; }
    
    // Draw current scene- assumes you just reset GL for next frame
    void draw();
    
    // Run one cycle of all entities
    void cycle();
};

#endif

